#include <SDL/SDL.h>
#include <GL/gl.h>
#include <stdio.h>

int main ()
{
  SDL_Surface *screen;

  // Init SDL's video system (as usual).
  if (SDL_Init(SDL_INIT_VIDEO) != 0) {
    printf("Unable to initialize SDL: %s\n", SDL_GetError());
    return 1;
  }
  
  // Call SDL_Quite when the program exists.
  atexit(SDL_Quit);

  // Enable OpenGL double buffering.
  SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);

  // Set the color depth.
  SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 6);
  SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 6);
  SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 6);

  // Set the video mode with support for OpenGL Rendering.
  screen = SDL_SetVideoMode(640, 480, 16, SDL_OPENGL);
  if (screen == NULL) {
    printf("Unable to set video mode: %s\n", SDL_GetError());
    return 1;
  }

  // Set the window title.
  SDL_WM_SetCaption("OpenGL with SDL!", "OpenGL");

  // Now any OpenGL command can be used.
  glViewport(80, 0, 480, 480);
  
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  glFrustum(-1.0, 1.0, -1.0, 1.0, 1.0, 100.0);
  glClearColor(0, 0, 0, 0);
  
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  
  glBegin(GL_TRIANGLES);
  glColor3f(1.0, 0.0, 0.0);
  glVertex3f(0.0, 1.0, -2.0);
  glColor3f(0.0, 1.0, 0.0);
  glVertex3f(1.0, -1.0, -2.0);
  glColor3f(0.0, 0.0, 1.0);
  glVertex3f(-1.0, -1.0, -2.0);
  glEnd();

  glFlush();

  // Display the back buffer to the screen.
  SDL_GL_SwapBuffers();

  // Wait 5 seconds.
  SDL_Delay(5000);

  return 0;
}
